<?php
	
	include 'dbfields.php';
	
	# connect to db and stuff
	$con = mysql_connect('db431735193.db.1and1.com', 'dbo431735193', 'protein');
	if (!$con)
	{
		die('Could not connect: ' . mysql_error());
	}
	mysql_select_db("db431735193", $con);
	
	# process all arguments
	$numtuples = (count($_POST)-1))/3+1; // i THINK that the checkboxes are passed as a single array
										 // so its count would be 1 from the perspective of $_POST
	$desiredfields = $_POST["fields"];
	for ($i = 1; $i<$numtuples; $i+=1) {
		$crit[$i] = $_POST["crit" . $i];
		$rel[$i] = $_POST["rel" . $i];
		$val[$i] = $_POST["val" . $i];
	}
	
	$mysqlstring = "SELECT "
	
	// What fields?
	foreach $fieldname in $desiredfields {
		$mysqlstring .= $fieldname . ", ";
	}
	
	// remove trailing ', '
	$mysqlstring = substr($mysqlstring, 0, strlen($mysqlstring)-2);
	
	
	
	$mysqlstring .= " FROM table WHERE ";
	for ($i = 1; $i<$numtuples; $i+=1) {
		$mysqlstring = $mysqlstring . "(" . $crit[$i] . $rel[$i] . $val[$i] . ") AND" 
		$rel[$i] = $_POST["rel" . $i];
		$val[$i] = $_POST["val" . $i];
	}
	
	$mysqlstring = substr($mysqlstring, 0, strlen($mysqlstring)-4); #remove trailing AND
	$result = mysql_query($mysqlstring);
	
	echo "<tr><th>PDB Code</th>
	<th>delta-delta-G average</th>
	<th>Secondary Structure</th></tr>";
	
	while($row = mysql_fetch_array($result)) {
		echo "<tr>";
		echo "<td>" . $row['pdbcode'] . "</td>";
		echo "<td>" . $row['ddgavg'] . "</td>";
		echo "<td>" . $row['secstr'] . "</td>";
		echo "</tr>";
	}
	# submit jobs
	
>